package org.mybatis.spring.boot.study.dataFlow.importBDRegistrar.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;

/**
 * @author nicky
 * @date 2021/9/1 下午5:38
 */
public class TjsMapperScannerRegistrar implements BeanDefinitionRegistryPostProcessor, BeanNameAware {

    private String beanName;
    private String[] basePackages;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        //扫描注解
        //BeanDefinition beanDefinition = beanDefinitionRegistry.getBeanDefinition(this.beanName);
        //MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
        //String[] basePackages = (String[]) mutablePropertyValues.get("basePackages");

        //扫描类
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry, false);

        //只扫描@MyMapper
        scanner.addIncludeFilter(new AnnotationTypeFilter(MyMapper.class));
        scanner.scan(basePackages);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getBeanName() {
        return beanName;
    }

    public String[] getBasePackages() {
        return basePackages;
    }

    public void setBasePackages(String[] basePackages) {
        this.basePackages = basePackages;
    }
}
